//
//  ModelRepaymentDetail.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/19.
//

#import "ModelRepaymentDetail.h"

NSString *const kModelRepaymentDetailAmount = KEY_amount;
NSString *const kModelRepaymentDetailProductName = KEY_productName;
NSString *const kModelRepaymentDetailAppName = KEY_appName;
NSString *const kModelRepaymentDetailRepaymentCodeList = KEY_repaymentCodeList;
NSString *const kModelRepaymentDetailOrderNo = KEY_orderNo;
NSString *const kModelRepaymentDetailOpenPayCode = KEY_openPayCode;

@interface ModelRepaymentDetail ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelRepaymentDetail

@synthesize amount = _amount;
@synthesize productName = _productName;
@synthesize appName = _appName;
@synthesize repaymentCodeList = _repaymentCodeList;
@synthesize orderNo = _orderNo;
@synthesize openPayCode = _openPayCode;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
        self.amount = [[self objectOrNilForKey:kModelRepaymentDetailAmount fromDictionary:dict] doubleValue];
        self.productName = [self objectOrNilForKey:kModelRepaymentDetailProductName fromDictionary:dict];
        self.appName = [self objectOrNilForKey:kModelRepaymentDetailAppName fromDictionary:dict];
        NSObject *receivedRepaymentCodeList = [dict objectForKey:kModelRepaymentDetailRepaymentCodeList];
        NSMutableArray *parsedRepaymentCodeList = [NSMutableArray array];
        if ([receivedRepaymentCodeList isKindOfClass:[NSArray class]]) {
            for (NSDictionary *item in (NSArray *)receivedRepaymentCodeList) {
                if ([item isKindOfClass:[NSDictionary class]]) {
                    [parsedRepaymentCodeList addObject:[ModelRepaymentCodeList modelObjectWithDictionary:item]];
                }
            }
        } else if ([receivedRepaymentCodeList isKindOfClass:[NSDictionary class]]) {
            [parsedRepaymentCodeList addObject:[ModelRepaymentCodeList modelObjectWithDictionary:(NSDictionary *)receivedRepaymentCodeList]];
        }
        self.repaymentCodeList = [NSArray arrayWithArray:parsedRepaymentCodeList];
        self.orderNo = [self objectOrNilForKey:kModelRepaymentDetailOrderNo fromDictionary:dict];
        self.openPayCode = [ModelRepaymentCodeList modelObjectWithDictionary:[dict objectForKey:kModelRepaymentDetailOpenPayCode]];
    }
    return self;
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.amount] forKey:kModelRepaymentDetailAmount];
    [mutableDict setValue:self.productName forKey:kModelRepaymentDetailProductName];
    [mutableDict setValue:self.appName forKey:kModelRepaymentDetailAppName];
    NSMutableArray *tempArrayForRepaymentCodeList = [NSMutableArray array];
    for (NSObject *subArrayObject in self.repaymentCodeList) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            [tempArrayForRepaymentCodeList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            [tempArrayForRepaymentCodeList addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForRepaymentCodeList] forKey:kModelRepaymentDetailRepaymentCodeList];
    [mutableDict setValue:self.orderNo forKey:kModelRepaymentDetailOrderNo];
    [mutableDict setValue:[self.openPayCode dictionaryRepresentation] forKey:kModelRepaymentDetailOpenPayCode];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}

@end
